package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.jzweb.domain.BookOrderDO;
import com.jinzhi.jzweb.domain.ExaminationBatchComSDO;
import com.jinzhi.jzweb.domain.SysUserRelevancyDO;
import com.jinzhi.jzweb.service.ExaminationBatchComSService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.SysUserRelevancyService;
import com.jinzhi.sys.service.UserRoleService;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.ExaminationBatchComDO;
import com.jinzhi.jzweb.service.ExaminationBatchComService;
import com.jinzhi.common.utils.Result;

/**
 *
 * <pre>
 * 竞赛考试批次管理
 * </pre>
 * <small> 2023-05-07 14:59:39 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationBatchCom")
public class ExaminationBatchComController extends BaseController {
    @Autowired
    private ExaminationBatchComService examinationBatchComService;
    @Autowired
    SysUserRelevancyService userRelevancyService;
    @Autowired
    UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private ExaminationBatchComSService examinationBatchComServices;
    @GetMapping()
    @RequiresPermissions("jzweb:examinationBatchCom:examinationBatchCom")
    String ExaminationBatchCom(Integer type,Model model){
        model.addAttribute("type",type);
        return "jzweb/examinationBatchCom/examinationBatchCom";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:examinationBatchCom:examinationBatchCom")
    public Result<Page<ExaminationBatchComDO>> list(ExaminationBatchComDO examinationBatchComDTO){
        EntityWrapper<ExaminationBatchComDO> wrapper = new EntityWrapper<>();
        Page<ExaminationBatchComDO> page =null;
        // 查询列表数据
        if(examinationBatchComDTO.getType()==4){
            boolean isYes=false;
            //1650432312954884097;;1654825643938709505;;1655451892407132162
            //1628314089924603905;;1638449851768819713;;1641246520054296578
            System.out.println("user="+this.getUserId());
            List<String> organizationNewsDOS = userRoleService.selroleNameList(this.getUserId());
            //for(RoleDO li:listRole){
            for(String name:organizationNewsDOS){
                if(name.contains("管理员")){
                     isYes=true;
                     break;
                }
            }
            if(!isYes){
                wrapper.like("refereeB",this.getUserId().toString()).or().like("refereeC",this.getUserId().toString())
                        .like("name",examinationBatchComDTO.getName()).eq("type",examinationBatchComDTO.getType());
                page = examinationBatchComService.selectPage(getPage(ExaminationBatchComDO.class), wrapper);
            }else {
                page = examinationBatchComService.selectPage(getPage(ExaminationBatchComDO.class),
                        wrapper.eq("type",examinationBatchComDTO.getType()).like("name",examinationBatchComDTO.getName()).orderBy("id", false));
            }
        }else {
            page = examinationBatchComService.selectPage(getPage(ExaminationBatchComDO.class),
                    wrapper.eq("type",examinationBatchComDTO.getType()).like("name",examinationBatchComDTO.getName()).orderBy("id", false));
        }
        List<ExaminationBatchComDO> list = new ArrayList();
        for (int i = 0; i < page.getRecords().size(); i++) {
            ExaminationBatchComDO obj = (ExaminationBatchComDO) page.getRecords().get(i);


            list.add(obj);

        }
        int p=list.size();
        for (int i = 0; i < p; i++) {

            List<ExaminationBatchComSDO>  lists= examinationBatchComServices.findByKv("pid",list.get(i).getId());
            for(ExaminationBatchComSDO  li:lists ){
                ExaminationBatchComDO sdo=new ExaminationBatchComDO();
                sdo.setName(li.getName());
                sdo.setPid(list.get(i).getId());
                sdo.setId(li.getId());
                sdo.setState(1);
                sdo.setCbatchId(list.get(i).getCbatchId());

                sdo.setType(li.getType());
                sdo.setStartTime(li.getStartTime());
                sdo.setEndTime(li.getEndTime());
                list.add(sdo);
            }

        }


        page.setRecords(list);
        return Result.ok(page);
    }

    @Log("审核")
    @PostMapping("/review")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationBatch:review")
    public Result<String> review(@RequestParam("ids[]") Long[] ids, Integer state, String remark) {
        if (state.equals(2) || state.equals(1)) {
            int i = 0;
            for (Long id : ids) {
                ExaminationBatchComDO examinationBatchDO = examinationBatchComService.selectById(id);
                if (examinationBatchDO != null) {
                    examinationBatchDO.setState(state);
                    examinationBatchDO.setCheckerId(this.getUserId());
                    examinationBatchDO.setCheckerDate(new Date());
                    if (state.equals(2)) {
                        examinationBatchDO.setRemark(remark);//未通过，添加备注
                    } else {
                        examinationBatchDO.setRemark("");//审核通过，清空备注
                    }
                    boolean b = examinationBatchComService.updateById(examinationBatchDO);
                    if (b) {
                        i++;
                    } else {
                        break;
                    }
                }
            }
            if (i == ids.length) {
                return Result.build(0, "已成功审核：" + i + "条数据");
            } else {
                return Result.fail("审核状态修改失败！");
            }
        } else {
            return Result.fail("审核状态修改失败！");
        }
    }


    @GetMapping("/add")
    @RequiresPermissions("jzweb:examinationBatchCom:add")
    String add(){
        return "jzweb/examinationBatchCom/add";
    }

    @GetMapping("/adds")
    @RequiresPermissions("jzweb:examinationBatchCom:add")
    String adds( Long id,Model model,Long cbatchId){
        model.addAttribute("id", id);
        model.addAttribute("cbatchId", cbatchId);
        return "jzweb/examinationBatchCom/adds";
    }


//    @GetMapping("/adds")
//    @RequiresPermissions("jzweb:examinationBatchCom:add")
//    String adds(){
//
//        return "jzweb/examinationBatchCom/adds";
//    }


    @GetMapping("/edits")
    @RequiresPermissions("jzweb:examinationBatchCom:edit")
    String edits(Long id,Model model,Long cbatchId){
        ExaminationBatchComSDO examinationBatchCom = examinationBatchComServices.selectById(id);
        model.addAttribute("cbatchId", cbatchId);
        model.addAttribute("examinationBatchCom", examinationBatchCom);
        return "jzweb/examinationBatchCom/edits";
    }



    @GetMapping("/edit/{id}")
    @RequiresPermissions("jzweb:examinationBatchCom:edit")
    String edit(@PathVariable("id") Long id,Model model){
        ExaminationBatchComDO examinationBatchCom = examinationBatchComService.selectById(id);

        model.addAttribute("examinationBatchCom", examinationBatchCom);
        return "jzweb/examinationBatchCom/edit";
    }

    @Log("添加考试批次")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("jzweb:examinationBatchCom:add")
    public Result<String> save( ExaminationBatchComDO examinationBatchCom){


        examinationBatchComService.insert(examinationBatchCom);
        return Result.ok();
    }

    @Log("查询对应赛道匹配的裁判")
    @ResponseBody
    @GetMapping("/getUserReferee")
    public Object getUserReferee( String track){
        if(StringUtils.isBlank(track)){
            return Result.fail("赛道不能为空！");
        }

//    List<SysUserRelevancyDO> list=    userRelevancyService.findByKv("track",track);
//        List<Map> list1=new ArrayList<>();
//        for(SysUserRelevancyDO li:list){
//            List<UserDO> listUser=    userService.findByKv("user_relevancy_id",li.getId());
//            Map map=new HashMap();
//            if(listUser.size()>0){
//                map.put("userId",listUser.get(0).getId());
//                map.put("name",listUser.get(0).getName());
//                list1.add(map);
//            }
//        }
        List<Map> list=  userRelevancyService.listUserRele(track);
        return Result.ok(list);
    }
    @Log("修改考试批次")
    @ResponseBody
    @RequestMapping("/updates")
    @RequiresPermissions("jzweb:examinationBatchCom:edit")
    public Result<String>  updates( ExaminationBatchComSDO examinationBatchCom){
        long m = ((examinationBatchCom.getEndTime().getTime()-examinationBatchCom.getStartTime().getTime())/1000/60);
            if(m<=0){
                return Result.fail("结束时间不能小于或等于开始时间！");
            }
        ExaminationBatchComSDO examinationBatchComSDO= examinationBatchComServices.findOneByKv("name",examinationBatchCom.getName().replaceAll(" ",""),"pid",examinationBatchCom.getIds(),"cbatch_id",examinationBatchCom.getCbatchId());
        if(examinationBatchComSDO!=null){
            if(!examinationBatchComSDO.getId().toString().equals(examinationBatchCom.getId().toString())){
                return Result.fail("赛场已存在！");
            }

        }
        boolean update = examinationBatchComServices.updateById(examinationBatchCom);
        return update ? Result.ok() : Result.fail();
    }


    @Log("修改考试批次")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("jzweb:examinationBatchCom:edit")
    public Result<String>  update( ExaminationBatchComDO examinationBatchCom){
        boolean update = examinationBatchComService.updateById(examinationBatchCom);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除考试批次")
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationBatchCom:remove")
    public Result<String>  remove( Long id){
        examinationBatchComService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除考试批次")
    @PostMapping( "/batchRemove")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationBatchCom:batchRemove")
    public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
        examinationBatchComService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

}
